<template>
	<view class="">
		<scroll-view scroll-y="true" enable-back-to-top class="scroll-box">
			<view class="con-c">
				<view class="user-info">
					<u-avatar class="avatar" :src="userInfo.headimg" level-bg-color="#8072f3" size="130"></u-avatar>
					<view class="user-c">
						<view class="username u-skeleton-fillet">{{userInfo.nickname}}</view>
						<view class="num-box u-skeleton-fillet">
							<text>{{follow_num}} <text class="txt">关注</text></text>
							<text>{{followed_num}} <text class="txt">粉丝</text></text>
							<text>{{userInfo.member_level_name}}</text>
						</view>
						<text class="desc u-skeleton-fillet">{{userInfo.intro}}</text>
						<!--动态的关注-->
						<u-button v-show="whetherFollow =='1'" :custom-style="customFollow" size="mini"
							style="float:right;font-size: 14px;" @click="cancelFollow">已关注</u-button>
						<u-button v-show="whetherFollow=='0'" :custom-style="customFollow" size="mini"
							style="float:right;font-size: 14px;" @click="follow">
							<u-icon name="plus" color="#fff"></u-icon>
							关注
						</u-button>
					</view>
				</view>
				<!-- tab -->
				<view class="tab-box">
					<u-tabs name="tab_name" :list="tabs" active-color="#616161" :is-scroll="false" :current="current"
						@change="tabChange"></u-tabs>
				</view>
				<!--我发布的圈子-->
				<view v-show="current === 0">
					<scroll-view scroll-y="true" @scrolltolower="loadMore" enable-back-to-top class="scroll-box">
						<view class="title u-m-b-30 u-font-35" v-if="topicList.length">创建的圈子</view>
						<view v-for="(item, index) in topicList" :key="index">
							<view class="my-topic u-flex u-p-30">
								<u-image width="120rpx" height="120rpx" :src="$util.img(item.cover_image)"></u-image>
								<view class="topic-info u-m-l-20">
									<view class="topic-title">{{item.topic_name}}</view>
									<view class="topic-desc u-tips-color u-m-t-40">{{item.description}}</view>
								</view>
							</view>
						</view>
						<u-loadmore v-if="topicList.length" height="80rpx" :status="loadStatus" icon-type="flower"
							color="#ccc" />
					</scroll-view>
				</view>
				<!--我发布的帖子-->
				<view v-show="current === 1">
					<scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box">
						<view class="post-list">
							<view v-for="(item, index) in postList" :key="index">
								<view class="post-item">
									<!--用户信息-->
									<view class="post-item-top-user">
										<view>
											<u-avatar class="avatar" :src="item.member_headimg"></u-avatar>
										</view>
										<view class="center">
											<view style="display: felx;align-items: center;">
												<text class="username">{{item.member_name}}</text>
											</view>
											<view class="time">
												{{ $util.timeStampTurnTime(item.create_time, 'time') }}
											</view>
										</view>
									</view>
									<!--帖子列表-->
									<view class="post-content" @click="postDetail(item.post_id)">
										<view class="post-text" v-if="item.title">{{item.title}}</view>
										<rich-text class="post-text" :nodes="item.content"></rich-text>
										<!-- 帖子类型 -->
										<view v-if="item.type == 1 || item.type ==0 || item.media[0]!==''">
											<!--一张图片-->
											<block v-if="item.media.length == 1">
												<image :lazy-load="true" mode="aspectFill" class="img-style-1"
													:src="$util.img(item.media[0])"></image>
											</block>
											<!--二张图片-->
											<block v-if="item.media.length == 2">
												<view class="img-style-2">
													<image :lazy-load="true" v-for="(mediaItem, index2) in item.media"
														:key="index2" mode="aspectFill" :src="$util.img(mediaItem)">
													</image>
												</view>
											</block>
											<!--三张以上图片-->
											<block v-if="item.media.length > 2">
												<view class="img-style-3">
													<image :lazy-load="true" v-for="(mediaItem, index2) in item.media"
														:key="index2" mode="aspectFill" :src="$util.img(mediaItem)">
													</image>
												</view>
											</block>
										</view>
										<!-- 视频 -->
										<view v-if="item.type == 2 && item.video!==null">
											<video :src="$util.img(item.video)" controls objectFit="cover"></video>
										</view>
										<!--评论点赞数-->
										<view class="footer u-flex u-row-between u-m-t-25">
											<view>
												<text>圈子|{{item.topic_name}}</text>
											</view>
											<view class="u-flex">
												<button @click.stop="onShare" class="u-reset-button" open-type="share">
													<u-icon name="zhuanfa"></u-icon>
													<text class="count">分享</text>
												</button>
												<view>
													<u-icon name="eye" size="36" ></u-icon>
													<text >{{item.read_count}}</text>
												</view>
												<view class="u-m-l-20">
													<u-icon name="heart" size="36" ></u-icon>
													<text >{{item.dianzan_num}}</text>
												</view>
											</view>
										</view>
									</view>

								</view>
							</view>
							<u-loadmore v-if="postList.length" height="80rpx" :status="loadStatus" icon-type="flower"
								color="#ccc" />
						</view>
					</scroll-view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>
<script>
	import Config from '../../common/js/config.js'
	import globalConfig from '@/common/js/golbalConfig.js';
	export default {
		data() {
			return {
				postList: [],
				topicList: [],
				userInfo: [],
				follow_num: '',
				followed_num: '',
				post_num: '',
				userInfo: '',
				token: "",
				tabs: [{
					tab_name: '主页'
				}, {
					tab_name: '帖子'
				}],
				current: 0,
				isEmpty: false,
				loadStatus: 'loadmore', //loadmore-加载前的状态，loading-加载中的状态，nomore-没有更多的状态
				lastPage: 1,
				currentPage: 1,
				memberId: '',
				followedId: '',
				whetherFollow: 0,
				customFollow: {
					backgroundColor: '#333',
					color: '#fff'
				}
			}
		},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();
		},
		onLoad(options) {
			console.log(options.followed_id);
			if (options.followed_id) {
				this.followedId = options.followed_id;
				this.goFollowCenter();
				this.getCirclePost();
				this.getFollowCircle();
			} else {
				this.$util.redirectTo('/newpages/social/index', {}, 'redirectTo');
			}
			//登录后查询
			this.token = uni.getStorageSync('token');
			//登录后查询
			if (uni.getStorageSync('userInfo')) {
				this.member_id = uni.getStorageSync('userInfo').member_id
			}
			this.getWhetherFollow();
		},
		onReachBottom() {
			if (this.currentPage < this.lastPage) {
				this.currentPage += 1;
				this.getMyPost();
			}
		},
		mixins: [globalConfig],
		methods: {
			onShare() {},
			//动态用户中心
			goFollowCenter() {
				this.loadStatus = 'loading';
				this.$api.sendRequest({
					url: '/social/api/member/count',
					data: {
						page: this.currentPage,
						member_id: this.followedId
					},
					success: res => {
						if (res.code == 0) {
							this.userInfo = res.data.userInfo,
								this.follow_num = res.data.follow_num,
								this.followed_num = res.data.followed_num
						}
					},
					fail: res => {
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},


			//关注的人户中心
			getFollowCircle() {
				this.loadStatus = 'loading';
				this.$api.sendRequest({
					url: '/social/api/member/usercircle',
					data: {
						page: this.currentPage,
						member_id: this.followedId
					},
					success: res => {
						if (res.code == 0) {
							this.topicList = [...this.topicList, ...res.data.data],
								this.lastPage = res.data.last_page;
							this.loadStatus = this.currentPage < res.data.last_page ? 'loadmore' :
								'nomore';
						}
					},
					fail: res => {
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},

			getFollowPost() {
				this.loadStatus = 'loading';
				this.$api.sendRequest({
					url: '/social/api/member/userpost',
					data: {
						page: this.currentPage,
						member_id: this.followedId
					},
					success: res => {
						if (res.code == 0) {
							this.postList = [...this.postList, ...res.data.data],
								this.lastPage = res.data.last_page;
							this.loadStatus = this.currentPage < res.data.last_page ? 'loadmore' :
								'nomore';
						}
					},
					fail: res => {
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},
			getCirclePost() {
				this.loadStatus = 'loading';
				this.$api.sendRequest({
					url: '/social/api/member/userpost',
					data: {
						page: this.currentPage,
						member_id: this.followedId
					},
					success: res => {
						if (res.code == 0) {
							this.postList = [...this.postList, ...res.data.data],
								this.lastPage = res.data.last_page;
							this.loadStatus = this.currentPage < res.data.last_page ? 'loadmore' :
								'nomore';
						}
					},
					fail: res => {
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},

			//检测是否关注
			getWhetherFollow() {
				this.$api.sendRequest({
					url: "/social/api/social/isfollow",
					data: {
						followed_id: this.followedId
					},
					success: res => {
						this.whetherFollow = res.data;
					}
				});
			},
			//动态页关注
			follow() {
				this.$api.sendRequest({
					url: "/social/api/social/follow",
					data: {
						member_id: uni.getStorageSync('userInfo').member_id,
						followed_id: this.memberId
					},
					success: res => {
						if (res.code == 0) {
							var data = res.data;
							if (data > 0) {
								this.whetherFollow = 1;
							}

						} else {
							this.$util.showToast({
								title: res.message
							});
							this.flag = false;
						}
					}
				});
			},
			//动态也用户取消关注
			cancelFollow() {
				this.$api.sendRequest({
					url: "/social/api/social/deletefollow",
					data: {
						followed_id: this.followedId
					},
					success: res => {
						var data = res.data;
						if (data > 0) {
							this.whetherFollow = 0;
						}
					}
				});
			},

			tabChange(index) {
				this.current = index;
			},
			loadMore() {
				if (this.currentPage < this.lastPage) {
					this.currentPage += 1;
					this.goFollowCenter();
					this.getCirclePost();
					this.getFollowCircle();
				}
			},

		}
	}
</script>

<style lang="scss" scoped>
	.tab-box {
		margin-top: -40px;
		margin-bottom: 30rpx;
	}

	//个人中心顶部样式
	.head-bg {
		width: 100%;
		height: 400rpx;
	}

	.con-c {
		padding: 30rpx;
		position: relative;
		width: 100%;
		background-color: #FFFFFF;
	}

	.user-info {
		display: flex;
		flex-direction: column;
		align-items: center;
		position: relative;
		height: 500rpx;
	}

	.avatar {
		width: 130rpx;
		height: 130rpx;
		border-radius: 50%;
		border: 2px solid #FFFFFF;
		z-index: 999;
	}

	.user-info .user-c {
		background-color: #FFFFFF;
		border-radius: 30rpx;
		padding: 30rpx;
		position: absolute;
		top: 70rpx;
		width: 100%;
		box-shadow: 5rpx 10rpx 20rpx #e6e6e6;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.user-info .user-c .username {
		font-size: 40rpx;
		font-weight: bold;
		margin-top: 70rpx;
	}

	.user-info .user-c .num-box {
		font-size: 24rpx;
		margin: 20rpx 0;
		text-align: center;
	}

	.user-info .user-c .num-box .txt {
		color: #999;
		margin-left: 5rpx;
	}

	.user-info .user-c .num-box text {
		margin-right: 30rpx;
	}

	.user-info .user-c .desc {
		font-size: 22rpx;
		color: #999;
	}

	.user-info .user-c .btn-box {
		margin-top: 20rpx;
	}

	.user-info .user-c .btn-box .btn {
		margin-right: 20rpx;
	}

	//帖子样式
	.scroll-box {
		height: 100vh !important;
		white-space: nowrap;
	}

	.my-topic {
		background-color: #FFFFFF;
	}

	.post-list {
		padding-bottom: 250px;
	}

	.post-item {
		background: #fff;
		padding: 20rpx;
		margin-bottom: 20rpx;

		.post-content {
			margin-top: 20rpx;

			.img-style-1 {
				display: block;
				width: 75%;
				max-height: 326rpx;
				border-radius: 5px;
				overflow: hidden;
			}

			.img-style-2 {
				display: flex;

				image {
					margin: 5rpx;
					border-radius: 5px;
					width: 100%;
					height: 294rpx;
				}
			}

			.img-style-3 {
				display: flex;
				flex-wrap: wrap;

				image {
					width: 31.3%;
					height: 200rpx;
					margin: 1%;
					border-radius: 5px;
				}
			}
		}

	}

	//帖子用户信息
	.post-item-top-user {
		display: flex;
		align-items: center;

		.avatar {
			width: 85rpx;
			height: 85rpx;
			border-radius: 50%;
			margin-right: 20rpx;
		}

		.center {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: 24rpx;
			color: #999;

			.username {
				font-size: 32rpx;
				font-weight: 600;
				color: #616161;
			}


		}

	}

	//帖子内容
	.post-text {
		display: block;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 10;
		white-space: pre-wrap;
		overflow: hidden;
	}
	.p-footer {
		display: flex;
		margin: 20rpx 0;
	
		.p-item {
			//margin: 0 auto;
			color: #616161;
			display: flex;
			align-items: center;
	
			.count {
				margin-left: 10rpx;
				font-size: 28rpx;
			}
		}
	
		.p-item[hidden] {
			display: none !important;
		}
	}
</style>
